package com.sunday.haowushop.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.adapter.CommenAdapter;
import com.sunday.common.adapter.ViewHolder;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.PixUtils;
import com.sunday.common.utils.SpannalbeStringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.common.widgets.NoScrollListview;
import com.sunday.haowushop.R;
import com.sunday.haowushop.common.BaseApp;
import com.sunday.haowushop.entity.Member;
import com.sunday.haowushop.entity.ShareRecord;
import com.sunday.haowushop.entity.ShareYunbiDetail;
import com.sunday.haowushop.http.ApiClient;
import com.sunday.haowushop.widgets.YunbiShareWindow;

import java.math.RoundingMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by 刘涛 on 2017/7/10.
 */

public class YunbiShareDetailActivity extends BaseActivity {


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.left_menu)
    ImageView leftMenu;
    @Bind(R.id.user_logo)
    CircleImageView userLogo;
    @Bind(R.id.share_num)
    TextView shareNum;
    @Bind(R.id.share_desc)
    TextView shareDesc;
    @Bind(R.id.share_money)
    TextView shareMoney;
    @Bind(R.id.share_time)
    TextView shareTime;
    @Bind(R.id.remain_num)
    TextView remainNum;
    @Bind(R.id.remain_time)
    TextView remainTime;
    @Bind(R.id.list_view)
    NoScrollListview listView;
    @Bind(R.id.btn_share)
    TextView btnShare;


    private ShareRecord shareRecord;
    private Member member;
    private String shareTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_yunbi_detail);
        ButterKnife.bind(this);
        title.setText("分享详情");
        leftMenu.setVisibility(View.VISIBLE);
        leftMenu.setImageResource(R.mipmap.back_white);
        shareRecord = (ShareRecord) getIntent().getSerializableExtra("shareRecord");
        shareTitle = BaseApp.getInstance().getMember().getNickName();
        member = BaseApp.getInstance().getMember();
        if (!TextUtils.isEmpty(member.getLogo())) {
            Glide.with(mContext)
                    .load(member.getLogo())
                    .error(R.mipmap.ic_logo_default)
                    .into(userLogo);
        }
        initView();
        getData();
    }


    private void initView(){
        if (shareRecord==null){return;}
        if (shareRecord.getStatus() != 2) {
            if (shareRecord.getNow() != 0) {
                btnShare.setVisibility(View.VISIBLE);
            }
        }
        shareNum.setText(String.format("分享给%d个好友", shareRecord.getTotal()));
        shareMoney.setText(SpannalbeStringUtils.setTextSizePx("¥", PixUtils.sp2px(mContext, 13)));
        shareMoney.append(SpannalbeStringUtils.setTextSizePx(String.format("%s",shareRecord.getTotalMoney().setScale(0, RoundingMode.HALF_UP))
                , PixUtils.sp2px(mContext, 22)));
        shareMoney.append(SpannalbeStringUtils.setTextSizePx("云币", PixUtils.sp2px(mContext, 13)));
        shareDesc.setText(shareRecord.getText());
        remark = shareRecord.getText();
        shareTime.setText(shareRecord.getCreateTime());
        remainNum.setText(String.format("已领取%1$d/%2$d个人", shareRecord.getTotal()-shareRecord.getNow(), shareRecord.getTotal()));
    }

    @OnClick(R.id.left_menu)
    void back() {
        onBackPressed();
    }


    private void getData() {
        showLoadingDialog(0);
        Call<ResultDO<ShareYunbiDetail>> call = ApiClient.getApiAdapter().getShareRecordDetail(shareRecord.getId());
        call.enqueue(new Callback<ResultDO<ShareYunbiDetail>>() {
            @Override
            public void onResponse(Call<ResultDO<ShareYunbiDetail>> call, Response<ResultDO<ShareYunbiDetail>> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    updateView(response.body().getResult());

                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }

            }

            @Override
            public void onFailure(Call<ResultDO<ShareYunbiDetail>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    private String remark;

    private void updateView(ShareYunbiDetail detail) {
        btnShare.setVisibility(View.GONE);
        if (detail.getShare() != null) {

        }
        if (detail.getList() != null && !detail.getList().isEmpty()) {
            listView.setAdapter(new CommenAdapter<ShareYunbiDetail.ListBean>(mContext, detail.getList(), R.layout.list_yunbi_share_item) {
                @Override
                public void convert(ViewHolder helper, ShareYunbiDetail.ListBean item, int position) {
                    if (!TextUtils.isEmpty(item.getLogo())) {
                        Glide.with(mContext)
                                .load(item.getLogo())
                                .error(R.mipmap.ic_logo_default)
                                .into((ImageView) helper.getView(R.id.img_money));
                    }
                    ((TextView) helper.getView(R.id.text_money)).setText(item.getNickName());
                    ((TextView) helper.getView(R.id.text_time)).setText(item.getTime());
                    ((TextView) helper.getView(R.id.text_extra)).setText(item.getStatus() == 0 ? "未使用" : item.getStatus() == 1 ? "已使用" : "已过期");
                    ((TextView) helper.getView(R.id.text_num)).setText(String.format("%s云币", item.getMoney().setScale(2, RoundingMode.HALF_UP)));
                    notifyDataSetChanged();
                }
            });

        }
    }

    @OnClick(R.id.btn_share)
    void share() {
        String shareUrl = String.format("%1$s%2$d-%3$d-%4$d-%5$d", ApiClient.SHARE_URL, 6, member.getId(), shareRecord.getId(), 0);
        YunbiShareWindow shareWindow = new YunbiShareWindow(mContext, shareUrl, String.format("%s给您发红包了", shareTitle), remark);
        shareWindow.showPopupWindow(leftMenu);
    }

}
